package com.ssyc.student.activity;

import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ssyc.common.base.BaseActivity;
import com.ssyc.common.base.BaseApplication;
import com.ssyc.common.base.SpKeys;
import com.ssyc.common.glide.ImageUtil;
import com.ssyc.common.http.HttpAdress;
import com.ssyc.common.http.HttpUtils;
import com.ssyc.common.manager.PopUpManager;
import com.ssyc.common.utils.GsonUtil;
import com.ssyc.common.utils.SPUtil;
import com.ssyc.common.utils.UiUtils;
import com.ssyc.common.view.ZzHorizontalProgressBar;
import com.ssyc.student.R;
import com.ssyc.student.bean.HomeInfo;
import com.ssyc.student.bean.PetPkInfo;

import java.util.HashMap;
import java.util.Map;

import de.hdodenhof.circleimageview.CircleImageView;
import okhttp3.Call;

/**
 * 学生端--宠物PK界面
 * Created by Administrator on 2018/5/7 0007.
 */

public class StudentPetPkActivity extends BaseActivity implements View.OnClickListener {
    private ImageView ivBack;
    //己方头像
    private CircleImageView ivHead;
    //己方智
    private ZzHorizontalProgressBar pbZhi;
    private TextView tvZhiNum;
    //己方勇
    private ZzHorizontalProgressBar pbYong;
    private TextView tvYongNum;
    //己方欢
    private ZzHorizontalProgressBar pbHuan;
    private TextView tvHuanNum;
    //己方爱
    private ZzHorizontalProgressBar pbAi;
    private TextView tvAiNum;

    //己方名称
    private TextView tvName;

    //PK方姓名
    private TextView tvPkName;
    //PK方头像
    private CircleImageView ivPkHead;
    //PK方智
    private ZzHorizontalProgressBar pbPkZhi;
    private TextView tvPkZhiNum;
    //PK方勇
    private ZzHorizontalProgressBar pbPkYong;
    private TextView tvPkYongNum;
    //PK方欢
    private ZzHorizontalProgressBar pbPkHuan;
    private TextView tvPkHuanNum;
    //PK方爱
    private ZzHorizontalProgressBar pbPkAi;
    private TextView tvPkAiNum;

    //PK按钮
    private CircleImageView ivPk;

    //我的宠物
    private ImageView iv_pk_mypet;

    //别人的宠物
    private ImageView iv_pk_otherpet;

    //定义己方和PK方分别对应的智勇欢爱的精力值
//    private int aZhi = 100, aYong = 200, aHuan = 100, aAi = 160;
//    private int pkZhi = 80, pkYong = 220, pkHuan = 120, pkAi = 130;

    //被PK对象的数据
    private HomeInfo info;
    @Override
    protected boolean isNeedShowTitle() {
        return false;
    }

    @Override
    protected String setTitleText() {
        return null;
    }

    @Override
    protected int getContentLayout() {
        return R.layout.student_actvity_pet_pk;
    }

    @Override
    protected void init() {
        //初始化视图
        initView();
        initIntent();
        showContent();
        initData();
    }

    /**
     * 接收数据
     */
    private void initIntent() {
        String json = getIntent().getStringExtra("json");
        Log.e("test","拜访："+json);
        if(json!=null&&!TextUtils.isEmpty(json)){
            info =  GsonUtil.jsonToBean(json, HomeInfo.class);
        }
    }

    /**
     * 初始化数据
     */
    private void initData() {
        //设置己方头像
        ImageUtil.displayImage(this, ivHead,
                SPUtil.getString(this, SpKeys.HEADIMG), R.drawable.defaultimage);

        //设置己方姓名
        tvName.setText(SPUtil.getString(this, SpKeys.NAME));

        //设置己方智勇欢爱
        pbZhi.setProgress(Integer.parseInt(SPUtil.getString(this, SpKeys.WISDOM)));
        tvZhiNum.setText(SPUtil.getString(this, SpKeys.WISDOM) + "/100");

        pbYong.setProgress(Integer.parseInt(SPUtil.getString(this, SpKeys.BRAVE)));
        tvYongNum.setText(SPUtil.getString(this, SpKeys.BRAVE) + "/100");

        pbHuan.setProgress(Integer.parseInt(SPUtil.getString(this, SpKeys.JOY)));
        tvHuanNum.setText(SPUtil.getString(this, SpKeys.JOY) + "/100");

        pbAi.setProgress(Integer.parseInt(SPUtil.getString(this, SpKeys.LOVE)));
        tvAiNum.setText(SPUtil.getString(this, SpKeys.LOVE) + "/100");


        //设置对方头像
        ImageUtil.displayImage(this, ivPkHead,
                info.getData().get(0).getStudent_icon(), R.drawable.defaultimage);

        //设置对方姓名
        tvPkName.setText(info.getData().get(0).student_name);

        //设置对方智勇欢爱
        pbPkZhi.setProgress(Integer.parseInt(info.getData().get(0).getWisdom()));
        tvPkZhiNum.setText(info.getData().get(0).getWisdom() +"/100");

        pbPkYong.setProgress(Integer.parseInt(info.getData().get(0).getBrave()));
        tvPkYongNum.setText(info.getData().get(0).getBrave() +"/100");

        pbPkHuan.setProgress(Integer.parseInt(info.getData().get(0).getJoy()));
        tvPkHuanNum.setText(info.getData().get(0).getJoy() +"/100");

        pbPkAi.setProgress(Integer.parseInt(info.getData().get(0).getLove()));
        tvPkAiNum.setText(info.getData().get(0).getLove() +"/100");

        //设置双方宠物类型
        setPetType();

    }

    /**
     * 设置宠物类型
     */
    private void setPetType() {
        //设置己方宠物类型
        if ("1".equals(SPUtil.getString(this, SpKeys.SERVICEPETTYPE))) {
            //狗
            iv_pk_mypet.setImageDrawable(getResources().getDrawable(R.drawable.stu_pet_dog));
        } else if ("2".equals(SPUtil.getString(this, SpKeys.SERVICEPETTYPE))) {
            //猫
            iv_pk_mypet.setImageDrawable(getResources().getDrawable(R.drawable.stu_pet_cat));
        } else if ("3".equals(SPUtil.getString(this, SpKeys.SERVICEPETTYPE))) {
            //怪物
            iv_pk_mypet.setImageDrawable(getResources().getDrawable(R.drawable.stu_pet_monster));
        } else if ("4".equals(SPUtil.getString(this, SpKeys.SERVICEPETTYPE))) {
            //熊猫
            iv_pk_mypet.setImageDrawable(getResources().getDrawable(R.drawable.stu_pet_panda));
        }

        //设置对方宠物类型
        if ("1".equals(info.getData().get(0).getPet_id())) {
            //狗
            iv_pk_otherpet.setImageDrawable(getResources().getDrawable(R.drawable.stu_pet_dog));
        } else if ("2".equals(info.getData().get(0).getPet_id())) {
            //猫
            iv_pk_otherpet.setImageDrawable(getResources().getDrawable(R.drawable.stu_pet_cat));
        } else if ("3".equals(info.getData().get(0).getPet_id())) {
            //怪物
            iv_pk_otherpet.setImageDrawable(getResources().getDrawable(R.drawable.stu_pet_monster));
        } else if ("4".equals(info.getData().get(0).getPet_id())) {
            //熊猫
            iv_pk_otherpet.setImageDrawable(getResources().getDrawable(R.drawable.stu_pet_panda));
        }
    }

    /**
     * 初始化视图的方法
     */
    public void initView() {
        ivBack = findViewById(R.id.iv_back);
        ivBack.setOnClickListener(this);

        //己方参数
        ivHead = findViewById(R.id.iv_head);
        tvName = findViewById(R.id.tv_name);

        pbZhi = findViewById(R.id.pb_zhi);
        tvZhiNum = findViewById(R.id.tv_zhi_num);
        pbZhi.setProgress(33);
        pbZhi.setBgColor(getResources().getColor(R.color.white));
        pbZhi.setProgressColor(getResources().getColor(R.color.st_zhi));


        pbYong = findViewById(R.id.pb_yong);
        tvYongNum = findViewById(R.id.tv_yong_num);
        pbYong.setProgress(33);
        pbYong.setBgColor(getResources().getColor(R.color.white));
        pbYong.setProgressColor(getResources().getColor(R.color.st_yong));


        pbHuan = findViewById(R.id.pb_huan);
        tvHuanNum = findViewById(R.id.tv_huan_num);
        pbHuan.setProgress(33);
        pbHuan.setBgColor(getResources().getColor(R.color.white));
        pbHuan.setProgressColor(getResources().getColor(R.color.st_hu));

        pbAi = findViewById(R.id.pb_ai);
        tvAiNum = findViewById(R.id.tv_ai_num);
        pbAi.setProgress(33);
        pbAi.setBgColor(getResources().getColor(R.color.white));
        pbAi.setProgressColor(getResources().getColor(R.color.st_ai));


        iv_pk_mypet = findViewById(R.id.iv_pk_mypet);

        iv_pk_otherpet = findViewById(R.id.iv_pk_otherpet);

        //PK方
        tvPkName = findViewById(R.id.tv_pk_name);
        ivPkHead = findViewById(R.id.iv_pk_head);
        pbPkZhi = findViewById(R.id.pb_pk_zhi);
        tvPkZhiNum = findViewById(R.id.tv_pk_zhi_num);
        pbPkZhi.setProgress(65);
        pbPkZhi.setBgColor(getResources().getColor(R.color.st_zhi));
        pbPkZhi.setProgressColor(getResources().getColor(R.color.white));

        pbPkYong = findViewById(R.id.pb_pk_yong);
        tvPkYongNum = findViewById(R.id.tv_pk_yong_num);
        pbPkYong.setProgress(65);
        pbPkYong.setBgColor(getResources().getColor(R.color.st_yong));
        pbPkYong.setProgressColor(getResources().getColor(R.color.white));

        pbPkHuan = findViewById(R.id.pb_pk_huan);
        tvPkHuanNum = findViewById(R.id.tv_pk_huan_num);
        pbPkHuan.setProgress(65);
        pbPkHuan.setBgColor(getResources().getColor(R.color.st_hu));
        pbPkHuan.setProgressColor(getResources().getColor(R.color.white));

        pbPkAi = findViewById(R.id.pb_pk_ai);
        tvPkAiNum = findViewById(R.id.tv_pk_ai_num);
        pbPkAi.setProgress(65);
        pbPkAi.setBgColor(getResources().getColor(R.color.st_ai));
        pbPkAi.setProgressColor(getResources().getColor(R.color.white));

        //PK图片
        ivPk = findViewById(R.id.iv_pk);
        ivPk.setOnClickListener(this);
    }




    /**
     * 点击事件
     *
     * @param v
     */
    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.iv_back) {
            //返回
            finish();
        } else if (id == R.id.iv_pk) {
            //PK点击事件
            http();
        }
    }

    /**
     * 宠物PK结果
     */
    private void http() {

        String acc = SPUtil.getString(this, SpKeys.ACCOUNT);
        String token = SPUtil.getString(this, SpKeys.TOKEN1);

        Map<String, String> map = new HashMap<>();
        map.put("type", "12");
        map.put("acc", acc);
        map.put("acc2", info.getData().get(0).getUser_id());
        map.put("fromname",SPUtil.getString(this, SpKeys.NAME));
        map.put("toname", info.getData().get(0).student_name);
        map.put("role", "0");
        map.put("platform", "8");
        map.put("apptoken", token);

//        Log.i("test","pk:"+"https://s.moregolden.com/ycsj_platform_nce2/HengMsg?type=12&acc="+
//                acc+"&acc2="+info.getData().get(0).getUser_id()+"&fromname="+SPUtil.getString(this, SpKeys.NAME)
//        +"&toname="+info.getData().get(0).student_name+"&role=0&platform=8&apptoken="+token);

        HttpUtils.post(HttpAdress.PK, map, this, new HttpUtils.CallBack() {
            @Override
            public void onError(Call call, Exception e, int id) {
                Log.i("test", "错误");
                UiUtils.Toast(BaseApplication.ERROR, false);
            }

            @Override
            public void onResponse(String response, int id) {
                showContent();
                PetPkInfo info = null;

                try {
                    info = GsonUtil.jsonToBean(response, PetPkInfo.class);
                } catch (Exception e) {
                    e.printStackTrace();
                    Log.i("test", "数据解析异常");
                }

                if (null != info) {
                    if ("200".equals(info.state)) {
                        //可PK
                        // 1赢 0输 2平
                        if (info.data == 1) {
                            if (!"".equals(info.name)) {
                                //连赢成就
                                shopMessagePop("1", info.name);
                            } else {
                                shopMessagePop("1", "");
                            }
                        } else if (info.data == 0) {
                            shopMessagePop("0", "");
                        } else if (info.data == 2) {
                            shopMessagePop("2", "");
                        }

                        //显示对方属性值
                        pbPkZhi.setVisibility(View.VISIBLE);
                        tvPkZhiNum.setVisibility(View.VISIBLE);

                        pbPkYong.setVisibility(View.VISIBLE);
                        tvPkYongNum.setVisibility(View.VISIBLE);

                        pbPkHuan.setVisibility(View.VISIBLE);
                        tvPkHuanNum.setVisibility(View.VISIBLE);

                        pbPkAi.setVisibility(View.VISIBLE);
                        tvPkAiNum.setVisibility(View.VISIBLE);

                    } else if ("-1".equals(info.state)) {
                        UiUtils.Toast("操作失败", false);
                    } else if ("10086".equals(info.state)) {
                        UiUtils.Toast("今日PK次数已达上限", false);
                    } else if ("10087".equals(info.state)) {
                        UiUtils.Toast("对方今日PK次数已达上限", false);
                    } else if ("10088".equals(info.state)) {
                        UiUtils.Toast("本人精力值过低", false);
                    } else if ("10089".equals(info.state)) {
                        UiUtils.Toast("对方精力值过低", false);
                    } else if ("10090".equals(info.state)) {
                        UiUtils.Toast("对方今日已被本人PK", false);
                    }
                }
            }
        });
    }

    /**
     * 弹出PK结果
     */
    private void shopMessagePop(String data, String honur) {

        if ("1".equals(data)) {
            //赢
            PopUpManager.showPop(this, R.layout.student_pop_pk_win, 0.3f, this, new PopUpManager.onGetViewListener() {
                @Override
                public void getChildView(View view, int layoutResId) {

                    //头像
                    ImageView iv_head = view.findViewById(R.id.iv_head);
                    ImageUtil.displayImage(getBaseContext(), iv_head,
                            SPUtil.getString(getBaseContext(), SpKeys.HEADIMG), R.drawable.defaultimage);
                    TextView tv_honur = view.findViewById(R.id.tv_honur);
                    if(!"".equals(honur)){
                        tv_honur.setVisibility(View.VISIBLE);
                        tv_honur.setText("恭喜！获得成就："+honur);
                    }
                }
            });
        }
        if ("0".equals(data)) {
            //输
            PopUpManager.showPop(this, R.layout.student_pop_pk_lose, 0.3f, this, new PopUpManager.onGetViewListener() {
                @Override
                public void getChildView(View view, int layoutResId) {

                    //头像
                    ImageView iv_head = view.findViewById(R.id.iv_head);
                    ImageUtil.displayImage(getBaseContext(), iv_head,
                            SPUtil.getString(getBaseContext(), SpKeys.HEADIMG), R.drawable.defaultimage);
                    RelativeLayout rl_bg = view.findViewById(R.id.rl_bg);
                    rl_bg.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            PopUpManager.dismiss();
                        }
                    });
                }
            });
        }
        if ("2".equals(data)) {
            //平
            PopUpManager.showPop(this, R.layout.student_pop_pk_deuce, 0.3f, this, new PopUpManager.onGetViewListener() {
                @Override
                public void getChildView(View view, int layoutResId) {

                    //头像
                    ImageView iv_head = view.findViewById(R.id.iv_head);
                    ImageUtil.displayImage(getBaseContext(), iv_head,
                            SPUtil.getString(getBaseContext(), SpKeys.HEADIMG), R.drawable.defaultimage);

                    RelativeLayout rl_bg = view.findViewById(R.id.rl_bg);
                    rl_bg.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            PopUpManager.dismiss();
                        }
                    });
                }
            });
        }
    }
}
